package com.example.wangyi.mvp.model;




import com.example.wangyi.callback.RxObserverCallBack;
import com.example.wangyi.di.component.DaggerOkComponent;
import com.example.wangyi.mvp.model.api.ApiService;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

public class RxOpretorImpl {
    @Inject
    ApiService mApiService;

    public RxOpretorImpl() {

        DaggerOkComponent.create().getSingleApiService(this);
    }

    //没有参数也没有请求头的get请求
    public <T> void rxGetRequest(String url, RxObserverCallBack<T> callBack) {
        RxOperator.threadtransformer(mApiService.requestGetData(url)).
                subscribe(new RxObserver(callBack));
    }

    //封装一个get请求的方法
    public <T> void rxGetRequest(String url, Map<String, T> params,
                                 RxObserverCallBack<T> callBack) {
        if (params != null && params.size() > 0) {
            RxOperator.threadtransformer(mApiService.requestGetData(url, params))
                    .subscribe(new RxObserver(callBack));
        } else {
            rxGetRequest(url, callBack);
        }
    }

    public <T> void rxGetRequest(String url, HashMap<String, T> headers,
                                 RxObserverCallBack<T> callBack) {
        if (headers != null && headers.size() > 0) {
            RxOperator.threadtransformer(mApiService.requestGetData(url, headers))
                    .subscribe(new RxObserver(callBack));
        } else {
            rxGetRequest(url, callBack);
        }
    }


    //没有参数也没有请求头的post请求
    public <T> void rxPostRequest(String url, RxObserverCallBack<T> callBack) {
        RxOperator.threadtransformer(mApiService.requestPostData(url)).
                subscribe(new RxObserver(callBack));
    }
    //没有参数也没有请求头的post请求
    public <T> void rxPostRequest(String url, Map<String,String> params,RxObserverCallBack<T> callBack) {
        if (params!=null&&params.size()>0){
            RxOperator.threadtransformer(mApiService.requestPostData(url,params)).
                    subscribe(new RxObserver(callBack));
        }

    }
}